#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#define int long long
#define N 400
int main() {
    int a[N][N] = { 0 }; 
    int n, m, c;
    int s[N][N] = { 0 };
    scanf("%lld%lld%lld", &n, &m, &c);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            scanf("%lld", &a[i][j]);
            s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
        }
    }
    int t;
    scanf("%lld", &t);
    while (t--) {
        int x1, x2, y1, y2, k;
        scanf("%lld%lld%lld%lld%lld", &x1, &y1, &x2, &y2, &k);
        k = k % (c + 1);
        int sum = s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1] - ((x2 - x1 + 1) * (y2 - y1 + 1)) * k;
        printf("%lld\n", sum);
    }
    return 0;
}